package com.eqgis.eqtool.tmp;

import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import pub.devrel.easypermissions.EasyPermissions;

import static android.os.Build.VERSION_CODES.M;

public class Utils {

    protected static String[] needPermissions = {
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.RECORD_AUDIO
    };

    /**----------------------------------------------------------------------------------------*/
    //<editor-fold>
    /**
     * 检测权限
     * return true:已经获取权限
     * return false: 未获取权限，主动请求权限
     */
    private static boolean checkPermissions(String[] permissions, Activity activity) {
        return EasyPermissions.hasPermissions(activity, permissions);
    }

    /**
     * 申请权限
     */
    public static void requestPermissions(Activity activity) {
        if (Build.VERSION.SDK_INT < M) {
            Toast.makeText(activity, "系统版本过低", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!checkPermissions(needPermissions,activity)) {
            //没有授权，编写申请权限代码
            EasyPermissions.requestPermissions(
                    activity,
                    "为了应用的正常使用，请允许以下权限。",
                    0,needPermissions);
        }
    }

    public static void requestPermissions(Activity activity,String... permissions) {
        if (Build.VERSION.SDK_INT < M) {
            Toast.makeText(activity, "系统版本过低", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!checkPermissions(permissions,activity)) {
            //没有授权，编写申请权限代码
            EasyPermissions.requestPermissions(
                    activity,
                    "为了应用的正常使用，请允许以下权限。",
                    0,permissions);
        }
    }



    /**
     * 隐藏虚拟按键，并且全屏
     */
    public static void hideBottomUIMenu(Activity activity) {
        int flags;
        int curApiVersion = Build.VERSION.SDK_INT;
        // This work only for android 4.4+
        if(curApiVersion >= Build.VERSION_CODES.KITKAT){
            // This work only for android 4.4+
            // hide navigation bar permanently in android activity
            // touch the screen, the navigation bar will not show
//            flags = View.SYSTEM_UI_FLAG_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_IMMERSIVE
//                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
        }else{
            // touch the screen, the navigation bar will show
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
        }
// must be executed in main thread :)
        activity.getWindow().getDecorView().setSystemUiVisibility(flags);
    }

    /**
     * 隐藏顶部UI
     * @param activity
     */
    public static void hideTopUI(Activity activity){
        // 隐藏标题栏
        activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

}
